﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

namespace HDS.Common.Helper
{
    public static class StringHelper
    {
        /// <summary>
        /// 过滤SQL字符
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string FilterSql(this string source)
        {
            if (string.IsNullOrEmpty(source))
                return "";

            return source.Replace("{", "{{").Replace("}", "}}").Replace(@"\", @"\\").Replace("'", @"''");
        }

        /// <summary>
        /// 移除非字母，非数字的字符
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string RemoveNotLetterNumber(this string source)
        {
            string pattern = @"[^a-zA-z0-9]";
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection matches = regex.Matches(source);
            foreach (Match match in matches)
            {
                source = source.Replace(match.Value, " ");
            }

            return source.Trim();
        }

        /// <summary>
        /// 替换多个连续空格为一个空格
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ReplaceMultSpaceToOne(this string source)
        {
            return Regex.Replace(source.Trim(), @" +", " ");
        }

        /// <summary>
        /// 替换空格为空
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ReplaceAllSpaceToEmpty(this string source)
        {
            return Regex.Replace(source.Trim(), @" ", "");
        }

        /// <summary>
        /// 空值ToString()
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string NullToString(this string source)
        {
            if (string.IsNullOrEmpty(source))
                return string.Empty;

            return source;
        }

        /// <summary>
        /// 返回 "字符 (字符串1)"
        /// </summary>
        /// <param name="source"></param>
        /// <param name="source1"></param>
        /// <returns></returns>
        public static string Enclosed(this string source, string source1)
        {
            if (string.IsNullOrEmpty(source1))
            {
                return source;
            }

            if (string.IsNullOrEmpty(source))
            {
                return source1;
            }

            if (source.Equals(source1))
            {
                return source;
            }

            return string.Format(source + "({0})", source1);
        }



        public static bool IsChina(string countryCode)
        {
            var arr = new string[] { "CN","HK","MO","TW" };
            return arr.Contains(countryCode);
        }
    }
}
